package com.express.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.thymeleaf.util.StringUtils;

public class FaceUtils {

	private static Map<String, String> qqFace = new HashMap<String, String>();
	
	static {
		qqFace.put("/::)", "0.gif");
		qqFace.put("/::~", "1.gif");
		qqFace.put("/::B", "2.gif");
		qqFace.put("/::|", "3.gif");
		qqFace.put("/:8-)", "4.gif");
		qqFace.put("/::<", "5.gif");
		qqFace.put("/::S", "6.gif");
		qqFace.put("/::X", "7.gif");
		qqFace.put("/::Z", "8.gif");
		qqFace.put("/::'(", "9.gif");
		
		qqFace.put("/::-|", "10.gif");
		qqFace.put("/::@", "11.gif");
		qqFace.put("/::P", "12.gif");
		qqFace.put("/::D", "13.gif");
		qqFace.put("/::O", "14.gif");
		qqFace.put("/::(", "15.gif");
		qqFace.put("/::+", "16.gif");
		qqFace.put("/:--b", "17.gif");
		qqFace.put("/::Q", "18.gif");
		qqFace.put("/::T", "19.gif");
		
		qqFace.put("/:,@P", "20.gif");
		qqFace.put("/:,@-D", "21.gif");
		qqFace.put("/::d", "22.gif");
		qqFace.put("/:,@o", "23.gif");
		qqFace.put("/::g", "24.gif");
		qqFace.put("/:|-)", "25.gif");
		qqFace.put("/::!", "26.gif");
		qqFace.put("/::L", "27.gif");
		qqFace.put("/::>", "28.gif");
		qqFace.put("/::,@", "29.gif");
		
		qqFace.put("/:,@f", "30.gif");
		qqFace.put("/::-S", "31.gif");
		qqFace.put("/:?", "32.gif");
		qqFace.put("/:,@x", "33.gif");
		qqFace.put("/:,@@", "34.gif");
		qqFace.put("/::8", "35.gif");		
		qqFace.put("/:,@!", "36.gif");
		qqFace.put("/:!!!", "37.gif");		
		qqFace.put("/:xx", "38.gif");
		qqFace.put("/:bye", "39.gif");
		
		qqFace.put("/:wipe", "40.gif");
		qqFace.put("/:dig", "41.gif");
		qqFace.put("/:handclap", "42.gif");
		qqFace.put("/:&-(", "43.gif");
		qqFace.put("/:B-)", "44.gif");
		qqFace.put("/:<@", "45.gif");
		qqFace.put("/:@>", "46.gif");
		qqFace.put("/::-O", "47.gif");
		qqFace.put("/:>-|", "48.gif");
		qqFace.put("/:P-(", "49.gif");
		
		qqFace.put("/::'|", "50.gif");
		qqFace.put("/:X-)", "51.gif");
		qqFace.put("/::*", "52.gif");
		qqFace.put("/:@x", "53.gif");
		qqFace.put("/:8*", "54.gif");
		qqFace.put("/:pd", "55.gif");
		qqFace.put("/:<W>", "56.gif");
		qqFace.put("/:beer", "57.gif");
		qqFace.put("/:basketb", "58.gif");
		qqFace.put("/:oo", "59.gif");
		
		qqFace.put("/:coffee", "60.gif");
		qqFace.put("/:eat", "61.gif");
		qqFace.put("/:pig", "62.gif");
		qqFace.put("/:rose", "63.gif");
		qqFace.put("/:fade", "64.gif");
		qqFace.put("/:showlove", "65.gif");
		qqFace.put("/:heart", "66.gif");
		qqFace.put("/:break", "67.gif");
		qqFace.put("/:cake", "68.gif");
		qqFace.put("/:li", "69.gif");
		
		qqFace.put("/:bome", "70.gif");
		qqFace.put("/:kn", "71.gif");
		qqFace.put("/:footb", "72.gif");
		qqFace.put("/:ladybug", "73.gif");
		qqFace.put("/:shit", "74.gif");
		qqFace.put("/:moon", "75.gif");
		qqFace.put("/:sun", "76.gif");
		qqFace.put("/:gift", "77.gif");
		qqFace.put("/:hug", "78.gif");
		qqFace.put("/:strong", "79.gif");
		
		qqFace.put("/:wead", "80.gif");
		qqFace.put("/:share", "81.gif");
		qqFace.put("/:v", "82.gif");
		qqFace.put("/:@)", "83.gif");
		qqFace.put("/:jj", "84.gif");
		qqFace.put("/:@@", "85.gif");
		qqFace.put("/:bad", "86.gif");
		qqFace.put("/:Ivu", "87.gif");
		qqFace.put("/:no", "88.gif");
		qqFace.put("/:ok", "89.gif");
		
		qqFace.put("/:love", "90.gif");
		qqFace.put("/:<L>", "91.gif");
		qqFace.put("/:jump", "92.gif");
		qqFace.put("/:shake", "93.gif");
		qqFace.put("/:<O>", "94.gif");
		qqFace.put("/:circle", "95.gif");
		qqFace.put("/:kotow", "96.gif");
		qqFace.put("/:turn", "97.gif");
		qqFace.put("/:skip", "98.gif");
		qqFace.put("/:oY", "99.gif");
		
		qqFace.put("/:#-0", "100.gif");
		qqFace.put("/:hiphot", "101.gif");
		qqFace.put("/:kiss", "102.gif");
		qqFace.put("/:<&", "103.gif");
		qqFace.put("/:&>", "104.gif");
		
	}
	
	public static List<Map.Entry<String, String>> getQQFace() {
		List arrayList = new ArrayList(qqFace.entrySet());
		 Collections.sort(arrayList, new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {  
	            Map.Entry obj1 = (Map.Entry)o1;  
	            Map.Entry obj2 = (Map.Entry)o2; 
	            int num1 = Integer.valueOf(obj1.getValue().toString().split("\\.")[0]);
	            int num2 = Integer.valueOf(obj2.getValue().toString().split("\\.")[0]);
	            return num1 > num2 ? 1 : num1 == num2 ? 0 : -1;  
	        }  
		 }); 
	        
		return arrayList;
	}
	
	private static boolean isEmojiCharacter(int codePoint) {

		return ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
	}
	
	public static String filterEmoji(String source) {
		if (!StringUtils.isEmpty(source)) {
			for (int i = 0; i < source.length() - 1; i++) {
				int codePoint = Character.toCodePoint(source.charAt(i), source.charAt(i+1));

				if (isEmojiCharacter(codePoint)) {
					String emoji = Integer.toHexString(codePoint);
					emoji = "<span class='emoji emoji" + emoji + "'></span>";
					String tempStr = source.substring(i, i+2);
					source = source.replaceAll(tempStr, emoji);
					i = i + emoji.length() - 2;
				}
			}
		}
		return source;
	}
	
//	public static String filterQQFace(String source) {
//
//		if (isQqFace(source)) {
//			String baseSite = SettingUtils.get().getSiteUrl();
//			Iterator<String> iter = qqFace.keySet().iterator();
//			while (iter.hasNext()) {
//				String key = iter.next();
//				if (source.contains(key)) {
//					String imgUrl = baseSite + "/resources/admin/images/qqface/" + qqFace.get(key);
//					String replacement = "<img src='" + imgUrl + "'/>";
//					source = source.replace(key, replacement);
//				}
//			}
//		}
//
//		return source;
//	}
//
	/** 
     * 判断是否是QQ表情 
     *  
     * @param content 
     * @return 
     */  
    public static boolean isQqFace(String content) {  
        boolean result = false;  
        
        if (!StringUtils.isEmpty(content)) {
        	// 判断QQ表情的正则表达式  
	        String qqfaceRegex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";  
	        Pattern p = Pattern.compile(qqfaceRegex);  
	        Matcher m = p.matcher(content);  
	        if (m.matches()) {
	            result = true;  
	        }  
        }
        return result;  
    }  
}
